#include"../source/server.hpp"
std::vector<LoopThread> threads(2);
int next_loop=0;
uint64_t  _conn_id=0;
EventLoop base_loop;
LoopThreadPool* pool=nullptr;
std::unordered_map<uint64_t,PtrConnetcion>_conns;
void OnMessage(const PtrConnetcion&con,Buffer*data){
 DBG_LOG("Recv data: %s",data->ReadPosition());
 data->MoveReadOffset(data->ReadAbleSize());
std::string s=" I love you";
 con->Send(s.c_str(),s.size());
}
void OnConnected(const PtrConnetcion& connection){
    DBG_LOG("New connection from %d",connection->Id());
}

void ConnectionDestory(const PtrConnetcion& connection){
    _conns.erase(connection->Id());
}
void NewConnection(int sockfd){
    _conn_id++;
    PtrConnetcion connection=std::make_shared<Connection>(pool->NextLoop(),_conn_id,sockfd);
    connection->SetMessageCallBack(OnMessage);
    connection->SetServerCloseCallBack(ConnectionDestory);
    connection->SetConnectedCallBack(OnConnected);
    connection->EnableInactiveRelease(10);
    connection->Established();
    _conns[_conn_id]=connection;
    DBG_LOG("New connection id:%ld",_conn_id);
}
int main()
{
    srand(time(NULL));
    pool=new LoopThreadPool(&base_loop);
    pool->SetThreadNum(2);
    pool->Creat();
    //设置监听套接字
   Acceptor acceptor(&base_loop,8500);
    acceptor.SetAcceptCallBack(NewConnection);
    acceptor.Listen();
    base_loop.Start();
    return 0;
}